package com.tony.business.config;

import com.tony.business.interceptor.ApiInterceptor;
import com.tony.business.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author 96543
 * @date 2019/8/17 18:02
 */
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    ApiInterceptor apiInterceptor;
    @Autowired
    TokenInterceptor tokenInterceptor;
    @Value("${system.swagger.enable}")
    private boolean enable;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/swagger-ui.html");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (enable) {
            registry.addInterceptor(tokenInterceptor).excludePathPatterns("/login", "/imgCode/*", "/refreshToken"
                    , "/", "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
        } else {
            registry.addInterceptor(tokenInterceptor).excludePathPatterns("/login", "/imgCode/*", "/refreshToken");
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决 SWAGGER2 404报错
        if (enable) {
            registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
}
